<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<title>武汉理工大学出版社</title>
		<script src="js/jquery-3.2.1.min.js"></script>
		<script src="js/rem.js"></script>
		<link rel="stylesheet" href="css/reset.css">
		<link rel="stylesheet" href="css/page_div.css">
		<script src="js/v3.2.8/vue.global.prod.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/axios.min.js"></script>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body style="background-color: #fff;">
		<!-- 头部 -->
		<div id="header"></div>
		
		<div id="bootstrap"></div>
		
		<div class="sales_zone" id="app">
			<div class="sales_zone_cont" v-if="content==''">
				<!-- 基本信息 -->
				<div class="user_form">
					<ul>
						<li>
							<p class="key">书名</p>
							<div class="val">
								<input type="text" placeholder="请输入书名" v-model="bookName" @change="getList">
							</div>
						</li>
						<li>
							<p class="key">作者</p>
							<div class="val">
								<input type="text" placeholder="请输入作者" v-model="bookAuthor" @change="getList">
							</div>
						</li>
						<li>
							<p class="key">ISBN</p>
							<div class="val">
								<input type="text" placeholder="请输入ISBN" v-model="isbn" @change="getList">
							</div>
						</li>
						<li>
							<p class="key">内容</p>
							<div class="val">
								<input type="text" placeholder="请输入内容" v-model="introduction" @change="getList">
							</div>
						</li>
					</ul>
				</div>
				<div class="but" @click="getList">搜索</div>
			</div>
			<div class="book_list">
				<ul class="book_ul">
					<li v-for="(item,index) in bookList" @click="openDetail(item.id)">
						<img :src="imgUrl+item.coverImg">
						<div class="content">
							<p class="title">{{item.bookName}}</p>
							<p class="text">作者：{{item.bookAuthor}}</p>
							<p class="text">出版时间：{{item.publishTime}}</p>
							<p class="text">ISBN：{{item.isbn}}</p>
							<p class="text" style="margin: 0;">定价：<span>￥{{item.discountPrice}}</span></p>
						</div>
					</li>
				</ul>
				<div v-cloak class="app">
					<div v-show="total >= 1" class="pageContainer">
						<ul class="pagesInner">
<!--							<li class="page" @click="prevOrNext(-1)"><span aria-hidden="true">&lt;</span></li>-->
							<li class="page" v-for="(item, index) in pages" :key="index" :class="{actived: item == pageIndex}" @click="selectPage(item)">
								<span>{{item}}</span>
							</li>
<!--							<li class="page" @click="prevOrNext(1)"><span class="fa fa-chevron-right" aria-hidden="true">&gt;</span></li>-->
						</ul>
						共{{total}}页&nbsp;转到第&nbsp;
						<input type="text" v-model="goToPage" class="totalinput">
						&nbsp;页&nbsp;
						<button @click="handleGoToPage">GO</button>
					</div>
				</div>
				<!-- <div id="page" class="page_div"></div> -->
			</div>
			
			<!-- 加载中 -->
			<div class="load_page" v-show="isLoadingShow">
				<div class="content">
					<div class="item">
						<i class="loader --1"></i>
					</div>
				</div>
			</div>
		</div>
		
		<!-- 底部 -->
		<div id="footer"></div>

	</body>
	<script>
		function getQueryVariable(variable)
		{
			const query = window.location.search.substring(1);
			const vars = query.split("&");
			for (var i=0;i<vars.length;i++) {
				var pair = vars[i].split("=");
				if(pair[0] == variable){return pair[1];}
			}
			return(false);
		}
		const App = {
			data() {
				return {
					imgUrl:'http://dbs.wutp.com.cn/CM-API',
					pageIndex:1,
					pageSize:8,
					goToPage: '',
					total:1,
					bookList:[],
					bookName:'',
					bookAuthor:'',
					isbn:'',
					introduction:'',
					content:'',
					isLoadingShow:false
				}
			},
			computed: {
				pages: function () {
					// 每次最多显示5个页码
					var c = this.pageIndex
					var t = this.total
					var p = []
					for (var i = 1; i <= t; i++) {
						p.push(i)
					}
					var l = p.length
					if (l <= 5) { // 总页数<=5，显示全部页码
						return p
					} else if (l > 5 && c <= 4) { // 总页数>5 && 当前页面<=3
						return [1, 2, 3, 4, 5,'...', '尾页']
					} else if (l > 5&& c > 4 && c <= l - 3) { // 总页数 > 5 && 当前页面 > 3 && 当前页 < 总页数 - 3
						return [c - 3, c - 2, c - 1, c, c + 1,  '...', '尾页']
					} else { // 总页数 > 5 && 当前页面 > 3 && 当前页 > 总页数 - 3
						debugger
						return [ '首页',t - 3, t - 2, t - 1,t ]
					}
				},
			},
			mounted(){
				sessionStorage.setItem('href',window.location.href);
				this.content = getQueryVariable("content")
				this.getBooksList()
			},

			methods:{
				async getBooksList() {
					let that = this;
					that.isLoadingShow = true
					const orderParam={publish_time: false}
					await axios({
						method: "get",
						url: "http://dbs.wutp.com.cn/CM-API/booksApi/getBooksList?pageNum="+that.pageIndex+"&pageSize="+that.pageSize+"&content="+that.content,
						params: {orderParam:orderParam},
					}).then(function (res) {
						that.bookList = res.data.data.content;
						// 总条数除以10并向上取整得到总页数
						const total = res.data.data.total / that.pageSize;
						that.total = Math.ceil(total);
						// that.total = res.data.data.total;
						that.pageIndex = res.data.data.pageNum
						that.pageSize = res.data.data.pageSize;
						that.isLoadingShow = false
					})
				},
				prevOrNext: function(n) {
					this.pageIndex += n
					this.pageIndex <= 1 ? this.pageIndex = 1 : this.pageIndex > this.total ? this.pageIndex = this.total : null
					this.getBooksList();
				},
				selectPage: function(n) {
					if(n=='尾页'){
						n = this.total
					}
					if(n=='首页'){
						n = 1
					}
					if (n === this.pageIndex) return
					if (typeof n === 'string') return
					this.pageIndex = n;
					this.getBooksList();
				},
				handleGoToPage: function() {
					this.pageIndex = this.goToPage <= 1 ? 1 : this.goToPage - 0 >= this.total - 0 ? this.total :
							this.goToPage
					this.goToPage = this.pageIndex;
					this.getBooksList();
				},
				async getList(){
					let that = this;
					that.isLoadingShow = true
					const searchParam=[]
					if(that.bookName != ''){
						searchParam.push({"SEARCH_LIKE_book_name":that.bookName})
					}
					if(that.bookAuthor != ''){
						searchParam.push({"SEARCH_LIKE_book_author":that.bookAuthor})
					}
					if(that.isbn != ''){
						searchParam.push({"SEARCH_LIKE_isbn":that.isbn})
					}
					if(that.introduction != ''){
						searchParam.push({"SEARCH_LIKE_introduction":that.introduction})
					}
					const param = JSON.stringify(searchParam).replace("[","").replace("]","").replaceAll("},{",",")
					await axios({
						method: "get",
						url: "http://dbs.wutp.com.cn/CM-API/booksApi/searchBooks?pageNum="+that.pageIndex+"&pageSize="+that.pageSize,
						params: {searchParam:param,orderParam:{"order_no":true}},
					}).then(function (res) {
						that.bookList = res.data.data.content;
						console.log(res.data.data.content)
						// 总条数除以10并向上取整得到总页数
						const total = res.data.data.total / that.pageSize;
						that.total = Math.ceil(total);
						// that.total = res.data.data.total;
						that.pageIndex = res.data.data.pageNum
						that.pageSize = res.data.data.pageSize;
						that.isLoadingShow = false
					})
				},
				openDetail(id){
					let hrefs = window.location.href;
					window.location.href="reDetails.html?name=TSZX&id="+id+'&href='+hrefs;
				},
			}
		};
		Vue.createApp(App).mount('#app');
	</script>

	<style>
		input{
			border: none;
		}
		.sales_zone{
			display: flex;
			margin: 0 3rem;
		}
		.book_list{
			width: 78%;
			margin-left: 0.3rem;
		}
		.book_list .book_ul{
			display: flex;
			align-items: center;
			justify-content: space-between;
			flex-wrap: wrap;
			margin-bottom: 0.1rem;
		}
		.book_list .book_ul li{
			width: 45.6%;
			border: 0.01rem solid #ddd;
			padding: 0.14rem;
			display: flex;
			align-items: center;
			cursor: pointer;
			margin-bottom: 0.3rem;
		}
		.book_list .book_ul li img{
			width: 1.8rem;
			height: 2.4rem;
			margin-right: 0.3rem;
		}
		.book_list .book_ul li .content{
			flex: 1;
		}
		.book_list .book_ul li .content .title{
			font-size: 0.22rem;
			margin-bottom: 0.2rem;
			letter-spacing: 0.01rem;
			color: #333;
			font-weight: 600;
			line-height: 0.29rem;
		}
		.book_list .book_ul li .content .text{
			font-size: 0.17rem;
			letter-spacing: 0.01rem;
			margin-bottom: 0.16rem;
			color: #656565;
		}
		.book_list .book_ul li .content .text span{
			font-size: 0.24rem;
			font-weight: bold;
			letter-spacing: 0.01rem;
			color: #ae0e16;
			margin-right: 0.2rem;
		}
		.sales_zone_cont{
			flex: 1;
			background-color: #f3f3f3;
			height: 4.2rem;
		}
		.user_form{
			display: flex;
			justify-content: center;
			padding-top: 0.32rem;
		}
		
		.user_form ul li{
			display: flex;
			align-items: center;
			margin-bottom: 0.2rem;
		}
		.user_form ul li .key{
			width: 0.5rem;
			text-align: left;
			font-size: 0.16rem;
			line-height: 0.16rem;
		}
		.user_form ul li .val{
			width: 1.4rem;
			height: 0.5rem;
			border: 0.01rem solid #d2d2d2;
			border-radius: 0.06rem;
			display: flex;
			align-items: center;
			padding: 0 0.14rem;
			background-color: #fff;
		}
		.user_form ul li .val input{
			width: 100%;
			background:none;
		}
		.user_form .ul{
			display: flex;
			align-items: center;
			margin: 0 0.12rem;
			cursor: pointer;
		}
		.user_form .ul img{
			width: 0.18rem;
			height: 0.18rem;
			margin-right: 0.06rem
		}
		.user_form .ul p{
			font-size: 0.18rem;
			color: #333;
			line-height: 0.18rem;
		}
		.user_form .item{
			margin-right: 0.2rem;
		}
		.user_form ul li .val textarea{
			border: none;
			width: 100%;
			height: 100%;
			outline: none;
			background: none;
		}
		
		.but{
			width: 1rem;
			height: 0.46rem;
			background-color: #212f52;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 0.16rem;
			border-radius: 0;
			margin: auto;
		}
	</style>
</html>
